public class t22 {
    //剑指 Offer 22. 链表中倒数第k个节点
    public static void main(String[] args) {
        int len = 5;
        ListNode hand = new ListNode(1);
        ListNode root = hand;
        for (int i = 2; i <= len; i++) {
            hand.next = new ListNode(i);
            hand = hand.next;
        }
        show(root);
        int k = 2;
        show(getKthFromEnd(root,k));
    }

    public static ListNode getKthFromEnd(ListNode head, int k) {
        ListNode tmp = head;
        int len = 0;
        while (tmp.next!=null){
            len++;
            tmp = tmp.next;
        }
        len++;
        tmp = head;
        for(int i =0;i<len-k;i++){
            tmp = tmp.next;
        }
        return tmp;

    }
    public static void show(ListNode root){
        ListNode tmp = root;
        while (tmp.next!=null){
            System.out.print(tmp.val+"-->");
            tmp = tmp.next;
        }
        System.out.println(tmp.val);
    }
}